package com.dongdong.service.impl;

import com.dongdong.mapper.TbItemCatMapper;
import com.dongdong.model.TbItemCat;
import com.dongdong.model.TbItemCatExample;
import com.dongdong.service.ItemCatService;
import com.dongdong.utils.PageBean;
import com.dongdong.utils.PageResult;
import com.dongdong.vo.ItemCatVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<ItemCatVO> getItemCatByPid(Long pid) {
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andParentIdEqualTo(pid);
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(example);
        List<ItemCatVO> voList = tbItemCats.stream().map((x) -> {
            ItemCatVO vo = new ItemCatVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public PageResult<ItemCatVO> getItemCatPage(PageBean<ItemCatVO> itemCatVO) {


        PageHelper.startPage(itemCatVO.getPageNum(),itemCatVO.getPageSize());
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(null);
        PageInfo<TbItemCat> info = new PageInfo<>(tbItemCats);
        List<ItemCatVO> voList = tbItemCats.stream().map((x) -> {
            ItemCatVO vo = new ItemCatVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult<>(info.getPageNum(),info.getPageSize(),info.getTotal(),voList);
    }
}
